<?php
/**
 * 订单控制器
 * @author e5
 *
 */
class OrderController extends CommonController{
	private $db;
	public function __init(){
		parent::__init();
		$this->checkLogin();
		$this->db = K("Order");
	}
	/**
	 * 订单列表
	 */
	public function index(){
		//获得全部分类
		$cateData = K("Category")->all();
		//获得顶级分类
		$topCate = array();
		foreach ($cateData as $v){
			if ($v['pid'] == 0){
				$topCate[] = $v;
			}
		}
		$this->assign("topCate",$topCate);
		$this->assign("cateData",$cateData);
		//处理分页
		$uid = session("uid");
		$total = $this->db->where("vshop_user_uid={$uid}")->count();
		$pageData = "";
		$limit = "0,7";
		if ($total > 7){
			$page = new Page($total,7);
			$pageData = $page->show();
			$limit = $page->limit();
		}
		//处理筛选条件
		$where = "";
		$ow = isset($_GET['ow'])?intval($_GET['ow']):1000;
		switch ($ow){
			case 0:
				$where = " AND ostatus=0";
				break;
			case 1:
				$where = " AND ostatus=1";
				break;
			case 2:
				$where = " AND ostatus=2";
				break;
		}
		//统计订单数量
		$arr = array('total'=>$total);
		$arr['jx'] = $this->db->where("vshop_user_uid={$uid} AND ostatus=0")->count();
		$arr['wc'] = $this->db->where("vshop_user_uid={$uid} AND ostatus=1")->count();
		$arr['wx'] = $this->db->where("vshop_user_uid={$uid} AND ostatus=2")->count();
		$this->assign("orderNum",$arr);
		//获得订单数据
		$orderData = $this->db->where("vshop_user_uid={$uid}".$where)->limit($limit)->all();
		foreach ($orderData as $k=>$ov){
			$orderData[$k]['time'] = date("Y-m-d H:i:s",$ov['otime']);
			$orderData[$k]['status'] = $this->getOrderStatus($ov['ostatus']);
		}
		$this->assign("orderData",$orderData);
		$this->assign("pageData",$pageData);
		$this->dis("myList");
	}
	/**
	 * 处理订单状态
	 * @param int $status
	 */
	private function getOrderStatus($status){
		switch ($status){
			case 0:
				return "进行中";
				break;
			case 1:
				return "已完成";
				break;
			default:
				return '无效';
				break;
		}
	}
	/**
	 * 添加订单(确认提交订单页)
	 */
	public function add(){
		if (IS_POST){
			//添加定单
			if (!$this->db->addOrder()) $this->error($this->db->error);
			Cart::delAll();
			go(U("sendOrderDown"));
		}
		$uid = session("uid");
		//获得收货地址
		$address = K("Address")->where("vshop_user_uid={$uid}")->order("arisdefault DESC")->all();
		foreach ($address as $k=>$v){
			$address[$k]['area'] = $v['ararea'].$v['detailarea'];
		}
		$this->assign("address",$address);
		//获得购物车数据
		$cartData = Cart::getAllData();
		$gadb = K("GoodsAttr");
		foreach ($cartData['goods'] as $k=>$v){
			//处理商品属性
			foreach (explode(",", $v['combine']) as $c){
				$cartData['goods'][$k]['attrs'][] = $gadb->where("gaid={$c}")->getField("gavalue");
			}
			$cartData['goods'][$k]['attrs'] = implode(",", $cartData['goods'][$k]['attrs']);
			//处理价格
			$cartData['goods'][$k]['total'] = number_format($cartData['goods'][$k]['total'], 2);
		}
		$cartData['total'] = number_format(intval($cartData['total']),2);
		$this->assign("cartData",$cartData);
		$this->dis('checkout');
	}
	/**
	 * 成功提交订单
	 */
	public function sendOrderDown() {
		$this->dis("sendOrderDown");
	}
	/**
	 * 取消(删除)订单
	 */
	public function delOrder() {
		$oid = Q("get.oid",0,"intval");
		if ($oid == 0) go(U("index"));
		$uid = session("uid");
		if (!$this->db->where("oid={$oid} AND vshop_user_uid={$uid}")->delete()) go(U("index"));
		K("OrderList")->where("vshop_order_oid={$oid}")->delete();
		$this->success("删除订单成功",U("index"));
	}
}